<?php

class MAIArtworks extends FTAObject {
		
		var $renderArtworksFunction;
		var $artwork;
		var $nextArtworkTitle;
		var $previousArtworkTitle;
	
		function MAIArtworks(){
			parent::__construct();
				
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";
		}
		
		function getArtworkByUrlFragment($urlfragment){
			if (is_null($urlfragment))
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docArtwork');
			
			$this->cmsDocument->addConstraint('title',$urlfragment);
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				$this->artwork = $this->cmsDocument->nextDocumentFromCache();
				return true;
			} else {
				$this->artwork = array();		
				return false;
			}
		}
		
		function getArtworkList($categoryCode = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docArtwork');
			$this->cmsDocument->addConstraint('extra13', '1');
					
			if(!is_null($categoryCode)){
				$this->cmsDocument->addConstraint('categorycode', $categoryCode);
			}
			
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("<<md.rank>>");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;
						
		}
		
		function renderArtworkList($categoryCode = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
			$artworks = $this->getArtworkList($categoryCode, $pageSize, $pageNumber, $sort, $direction);
			
			if(is_null($this->renderArtworksFunction)) {
				
				global $MAIArtworksItemRenderFunction;
				if(!isset($MAIArtworksItemRenderFunction)){
					$MAIArtworksItemRenderFunction = '$o=mai_getSharedArtworksObject(); echo $o->renderArtworksFunction($item);';
				}
				//echo "Into Here!";
				$this->renderArtworksFunction  = create_function('$item', $MAIArtworksItemRenderFunction );
			}
			
			if(count($artworks) > 0){	
				//we need to map the links and do some funky scope wrandling..
				mai_artworks_widgetList($artworks,"artwork-list", null, null, null,$this->renderArtworksFunction);
			} else {
				echo "No artworks currently available<br /><br />";
			}
		}
		
		function previousNextArtworkByUrl($urlfragment = null){
			
			if(is_null($urlfragment)){
				return false;
			}
			
			$this->getArtworkByUrlFragment($urlfragment);
			$artwork = $this->artwork;
			
			$artworkList = $this->getArtworkList($artwork->categorycode);
			
			$previousTitle = null;
			$nextTitle = null;
			
			$firstItem = true;
			$previousItem = null;
			$currentItem = null;
			$nextItem = null;
			$nextItemFlag = false;
			
			foreach ($artworkList as $item){
				
				if($firstItem){
					// Leave the previous item empty
					$firstItem = false;
				} else {
					$previousItem = $currentItem;
				}
				
				// Set the current Item
				$currentItem = $item;
				
				//if we are at the next item to be marked
				if($nextItemFlag){
					$nextItem = $currentItem;
					$nextTitle = $currentItem->title;
					$nextItemFlag = false;
					//don't need to go any further we have all the info
					break;
				}
				
				//test on if the currentitem is the one
				if($currentItem == $artwork){
					//mark previous item if it exists
					if(!is_null($previousItem)){
						$previousTitle = $previousItem->title;
					}
						
					//Set for the next item
					$nextItemFlag = true;
					
				}
				
				
			}
			
			//save and return the previous and next items
			$this->previousArtworkTitle = $previousTitle;
			$this->nextArtworkTitle = $nextTitle;
			
		}
}

	function mai_renderArtworkList($categoryCode = null, $pageSize = null, $pageNumber = null, $sort = null, $direction = null){
		ob_start();
		$artworkObject = mai_getSharedArtworksObject();
		$artworkObject->renderArtworkList($categoryCode, $pageSize, $pageNumber, $sort, $direction);
		return ob_get_clean();	
	}


	function mai_getSharedArtworksObject(){

		global $fta_;

		global $MAIArtworksClass;
		
		if(!is_null($MAIArtworksClass) && "MAIArtworks" != $MAIArtworksClass){
		} else {
			$MAIArtworksClass = "MAIArtworks";
		}
			
		if (!array_key_exists('maiArtworks',$fta_)){
			$fta_->maiArtworks = new  $MAIArtworksClass();
		}
		return $fta_->maiArtworks;
	}

	
	function mai_artworks_widgetList($items,$listClass="list",$altItemClass='altItem',$firstItemClass='firstItem', $lastItemClass='lastItem',$renderItemFunction) {
?>
<ul id="<?= $listClass ?>">
<?		
	$row = -1;
	$numRows = count($items);
	
	foreach($items as $item){
		
		$itemClasses=array();
		
		if ($altItemClass && 0 == ($row++) % 2 ) 
			array_push($itemClasses,$altItemClass);
		
		if ($firstItemClass && 0 == $row)
			array_push($itemClasses,$firstItemClass);			
			
		if ($lastItemClass && $numRows -1 == $row)
			array_push($itemClasses,$lastItemClass);

?>		
		<li <?= ( 0 < count($itemClasses) ? "class=\"" . implode(" ",$itemClasses) . "\"" : "") ?> ><?= $renderItemFunction( $item ) ?></li>		
<?		
	}
?>
</ul>
<?		
	}
?>